//
//  FieldHeader.m
//  SimpleHappy
//
//  Created by lanou on 15/11/24.
//  Copyright © 2015年 RockyFung. All rights reserved.
//

#import "FieldHeader.h"
#import "Define.h"
#import "SDCycleScrollView.h"
#import "DetailController.h"
#import "PictureModel.h"

@interface FieldHeader ()<SDCycleScrollViewDelegate>

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;

@property (nonatomic, strong) NSMutableArray *picArray; // 图片数组 URLString
@property (nonatomic, strong) NSMutableArray *titleArray; // 标题数组

@end


@implementation FieldHeader

- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        CGFloat width = KScreenWidth - 10;
        CGFloat height = width / 720 * 450;
        
        
        //网络加载 --- 创建带标题的图片轮播器
        self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(5, 5, width, height) imageURLStringsGroup:nil]; // 模拟网络延时情景
        self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        self.cycleScrollView.delegate = self;
//        self.cycleScrollView.titlesGroup = self.titleArray; // ===========给标题数组
        self.cycleScrollView.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
        self.cycleScrollView.placeholderImage = [UIImage imageNamed:@"placeholder"];
        //             --- 模拟加载延迟
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            self.cycleScrollView.imageURLStringsGroup = self.picArray; // ===============给网址数组
//        });

        [self.contentView addSubview:_cycleScrollView];

    }
    return self;
}

- (void)setModelArray:(NSMutableArray *)modelArray
{
    _modelArray = modelArray;
             // 初始化数组
        self.picArray = [NSMutableArray array];
        self.titleArray = [NSMutableArray array];

        // 赋值
        for (FieldHeaderModel *model in modelArray)
        {
            [self.picArray addObject:model.picUrl];
            [self.titleArray addObject:model.title];
        }
        self.cycleScrollView.titlesGroup = self.titleArray;
        self.cycleScrollView.imageURLStringsGroup = self.picArray;
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    DetailController *detailVc = [[DetailController alloc]init];
    detailVc.isDiscover = YES;
    FieldHeaderModel *model = self.modelArray[index];
    detailVc.targetUrl = [NSString stringWithFormat:@"https://m.yhouse.com/news-app/%@",model.linkId];
    
    // 获取控制器
    id object = [self nextResponder];
    while (![object isKindOfClass:[UIViewController class]] &&
           object != nil) {
        object = [object nextResponder];
    }
    UIViewController *uc=(UIViewController*)object;
    
    
    SHDetailModel *SHModel = [[SHDetailModel alloc]init];
    SHModel.picUrl = model.picUrl;
    SHModel.title = model.title;
    SHModel.openUrl = [NSString stringWithFormat:@"https://m.yhouse.com/news-app/%@",model.linkId];
    detailVc.SHDetailModel = SHModel;
    
    
    
    detailVc.isHead = YES;
    // 跳转
    detailVc.hidesBottomBarWhenPushed = YES;
    [uc.navigationController pushViewController:detailVc animated:YES];
    

}

@end
